package me.firstapp.manage.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.manage.common.BaseManage;
import me.firstapp.module.weixin.product.Product;
import me.firstapp.module.weixin.product.ProductCategory;
import me.firstapp.module.weixin.product.ProductSku;
import me.firstapp.module.weixin.product.RebateGrid;
import me.firstapp.module.weixin.product.Supplier;
import me.firstapp.service.weixin.product.ProductCategoryService;
import me.firstapp.service.weixin.product.ProductService;
import me.firstapp.service.weixin.product.ProductSkuService;
import me.firstapp.service.weixin.product.RebateGridService;
import me.firstapp.service.weixin.product.SupplierService;

@Controller
@RequestMapping(value = "/productManage")
public class ProductManagePage extends BaseManage {

	@Autowired
	private ProductCategoryService productCategoryService;

	@Autowired
	private ProductService productService;

	@Autowired
	private SupplierService supplierService;

	@Autowired
	private ProductSkuService productSkuService;

	@Autowired
	private RebateGridService rebateGridService;

	@RequestMapping(value = "/mobileFareList.html", method = RequestMethod.GET)
	public ModelAndView mobileFareListView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		return new ModelAndView("product/mobileFareList", modelMap);
	}

	@RequestMapping(value = "/mobileFareAdd.html", method = RequestMethod.GET)
	public ModelAndView mobileFareAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView("product/mobileFareAdd", modelMap);
	}

	@RequestMapping(value = "/kamiCategoryList.html", method = RequestMethod.GET)
	public ModelAndView kamiCategoryListView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		return new ModelAndView("product/kamiCategoryList", modelMap);
	}

	@RequestMapping(value = "/kamiCategoryAdd.html", method = RequestMethod.GET)
	public ModelAndView categoryAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView("product/kamiCategoryAdd", modelMap);
	}

	@RequestMapping(value = "/kami.html", method = RequestMethod.GET)
	public ModelAndView kamiView(Long kaMiProductId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("kaMiProductId", kaMiProductId);
		return new ModelAndView("product/kami", modelMap);
	}

	@RequestMapping(value = "/kamiAdd.html", method = RequestMethod.GET)
	public ModelAndView kamiAddView(Long kaMiProductId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("kaMiProductId", kaMiProductId);
		return new ModelAndView("product/kamiAdd", modelMap);
	}

	@RequestMapping(value = "/outProductList.html", method = RequestMethod.GET)
	public ModelAndView outProductListView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		return new ModelAndView("product/outProductList", modelMap);
	}

	@RequestMapping(value = "/outProductAdd.html", method = RequestMethod.GET)
	public ModelAndView outProductAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView("product/outProductAdd", modelMap);
	}

	@RequestMapping(value = "/importKamiFile.html", method = RequestMethod.GET)
	public ModelAndView importKamiFileView(Long kaMiProductId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("kaMiProductId", kaMiProductId);
		return new ModelAndView("product/importKami", modelMap);
	}

	@RequestMapping(value = "/prdtCategoryList.html", method = RequestMethod.GET)
	public ModelAndView prdtCategoryListView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		return new ModelAndView("product/prdtCategoryList", modelMap);
	}

	@RequestMapping(value = "/prdtCategoryUpdate.html", method = RequestMethod.GET)
	public ModelAndView prdtCategoryUpdate(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ProductCategory prdtCategory = productCategoryService.findById(id);
		modelMap.put("prdtCategory", prdtCategory);
		modelMap.put("categoryId", prdtCategory.getId());
		return new ModelAndView("product/prdtCategoryUpdate", modelMap);
	}

	@RequestMapping(value = "/prdtCategoryAdd.html", method = RequestMethod.GET)
	public ModelAndView prdtCategoryAddView(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		return new ModelAndView("product/prdtCategoryAdd", modelMap);
	}

	@RequestMapping(value = "/productList.html", method = RequestMethod.GET)
	public ModelAndView productListView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		List<ProductCategory> categoryList = productCategoryService.findAll();
		modelMap.put("categoryList", categoryList);
		return new ModelAndView("product/productList", modelMap);
	}

	@RequestMapping(value = "/productAdd.html", method = RequestMethod.GET)
	public ModelAndView productAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		List<ProductCategory> categoryList = productCategoryService.findProductCategorys();
		modelMap.put("categoryList", categoryList);
		return new ModelAndView("product/productAdd", modelMap);
	}

	@RequestMapping(value = "/productUpdate.html", method = RequestMethod.GET)
	public ModelAndView productUpdateView(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Product product = productService.findById(id);
		List<ProductCategory> categoryList = productCategoryService.findProductCategorys();
		ProductCategory defProductCategory = productCategoryService.findById(product.getProductCategoryId());
		modelMap.put("defProductCategory", defProductCategory);
		categoryList.remove(defProductCategory);
		modelMap.put("categoryList", categoryList);
		modelMap.put("product", product);
		modelMap.put("productId", id);
		int tuijian1 = product.getTuiJian();
		modelMap.put("tuijian1", tuijian1);
		if (tuijian1 == Product.TUIJIAN_ON) {
			modelMap.put("tuijian1Str", "是");
			modelMap.put("tuijian2", Product.TUIJIAN_OFF);
			modelMap.put("tuijian2Str", "否");
		} else if (tuijian1 == Product.TUIJIAN_OFF) {
			modelMap.put("tuijian1Str", "否");
			modelMap.put("tuijian2", Product.TUIJIAN_ON);
			modelMap.put("tuijian2Str", "是");
		}
		return new ModelAndView("product/productUpdate", modelMap);
	}

	@RequestMapping(value = "/suppliersList.html", method = RequestMethod.GET)
	public ModelAndView suppliersListView(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("id", id);
		return new ModelAndView("product/suppliersList", modelMap);
	}

	@RequestMapping(value = "/supplierAdd.html", method = RequestMethod.GET)
	public ModelAndView supplierAddView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView("product/supplierAdd", modelMap);
	}

	@RequestMapping(value = "/supplierUpdate.html", method = RequestMethod.GET)
	public ModelAndView supplierUpdateView(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Supplier supplier = supplierService.findById(id);
		modelMap.put("supplier", supplier);
		modelMap.put("id", supplier.getId());
		return new ModelAndView("product/supplierUpdate", modelMap);
	}

	@RequestMapping(value = "/productSkuList.html", method = RequestMethod.GET)
	public ModelAndView productSkuListView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("productId", productId);
		return new ModelAndView("product/productSkuList", modelMap);
	}

	@RequestMapping(value = "/productSkuAdd.html", method = RequestMethod.GET)
	public ModelAndView productSkuAddView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("productId", productId);
		return new ModelAndView("product/productSkuAdd", modelMap);
	}

	@RequestMapping(value = "/productSkuUpdate.html", method = RequestMethod.GET)
	public ModelAndView productSkuUpdateView(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("id", id);
		ProductSku productSku = productSkuService.findById(id);
		modelMap.put("productSku", productSku);
		modelMap.put("isDefault1", productSku.getIsDefault());
		if (ProductSku.DEFAULT_NOT == productSku.getIsDefault()) {
			modelMap.put("isDefault1Str", "非默认");
			modelMap.put("isDefault2", ProductSku.DEFAULT_YES);
			modelMap.put("isDefault2Str", "默认");
		} else if (ProductSku.DEFAULT_YES == productSku.getIsDefault()) {
			modelMap.put("isDefault1Str", "默认");
			modelMap.put("isDefault2", ProductSku.DEFAULT_NOT);
			modelMap.put("isDefault2Str", "非默认");
		}
		return new ModelAndView("product/productSkuUpdate", modelMap);
	}

	@RequestMapping(value = "/rebateGridList.html", method = RequestMethod.GET)
	public ModelAndView rebateGridListView(Long productId, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("productId", productId);
		return new ModelAndView("product/rebateGridList", modelMap);
	}

	@RequestMapping(value = "/rebateGridUpdate.html", method = RequestMethod.GET)
	public ModelAndView rebateGridUpdateView(Long id, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		modelMap.put("id", id);
		RebateGrid rebateGrid = rebateGridService.findById(id);
		modelMap.put("rebateGrid", rebateGrid);
		return new ModelAndView("product/rebateGridUpdate", modelMap);
	}
}
